<?php

namespace Biz\tools;

use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByPicAdvanceRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\Models\SearchByUrlRequest;
use AlibabaCloud\SDK\ImageSearch\V20210501\ImageSearch;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Psr7\Stream;
use Doraemon\pockets\constInterface\ApiCode;

class ImgSearch implements ApiCode
{
    const ACCESSKEYID = 'LTAI4G7ZePAG7GxL7cJRcXbL';
    const ACCESSKEYSERCRET = '7xNF6W5lssZpNaglV8gQHxvwqah3Rh';

    const ENDPOINT= 'imagesearch.cn-shanghai.aliyuncs.com';

    const PID = 'mm_1718650124_2251400212_111980150351';

    const CATEGORY = 9;//家具

    const REGIONID  = 'cn-shanghai';

     public  static  function getClient(){
        $config = new Config();
        $config->accessKeyId = self::ACCESSKEYID;
        $config->accessKeySecret = self::ACCESSKEYSERCRET;
        $config->regionId = self::REGIONID;
        $config->endpoint = self::ENDPOINT;
        $client = new ImageSearch($config);
        return $client;
    }



    public static  function searchByUrl($pic_url)
    {
        $config = new Config();
        $request = new SearchByUrlRequest();
        // 必填，图片URL
        $request->picUrl = $pic_url;
        // 选填，需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
//    $request->fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
        // 必填，PID
        $request->pid= self::PID;
        // 选填， 图片类目
        $request->categoryId = self::CATEGORY;
        // 选填，是否需要进行主体识别。默认true
        //$request->crop = false;
        // 选填，图片的主体区域，格式为x1,x2,y1,y2
        //$request->region = "518,1524,398,1632";
        // 选填，返回结果的起始位置。取值范围：0-499。默认值：0。
        $request->start = 0;
        // 选填，返回结果的数目。取值范围：1-20。默认值：10。
        $request->num = 10;
        // 选填，渠道ID。用于淘宝联盟中的渠道区分。
        //$request->relationId = 1145;
        $runtime = new RuntimeOptions();
        try {
            $response = self::getClient()->searchByUrlWithOptions($request,$runtime);
            $result = $response->toMap();
            return $result;
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
            return false;
        }
    }



   public static function searchByUploadPic($file_path = '')
    {
        $now = time();
        $request = new SearchByPicAdvanceRequest();
        // 必填，图片URL
        $request->picContentObject = new Stream(fopen($file_path, 'r+'));
        // 选填，需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
        // $request->fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
        $request->fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Url,ShopTitle,ItemId,SubTitle,ShopTitle";
        // 必填，PID
        $request->pid = "mm_1718650124_2251400212_111980150351";
        // 选填，图片类目
        //$request->categoryId = 88888888;
        // 选填，是否需要进行主体识别。默认true
        //$request->crop = false;
        // 选填，图片的主体区域，格式为x1,x2,y1,y2
        //$request->region = "518,1524,398,1632";
        // 选填，返回结果的起始位置。取值范围：0-499。默认值：0。
        $request->start = 0;
        // 选填，返回结果的数目。取值范围：1-20。默认值：10。
        $request->num = 10;
        // 选填，渠道ID。用于淘宝联盟中的渠道区分。
        //$request->relationId = 1145;
        $runtime = new RuntimeOptions();
        try {
            $response = _client()->searchByPicAdvance($request, $runtime);
            var_dump($response->toMap());
            var_dump(time() - $now , time() , $now);
        } catch (TeaUnableRetryError $e) {
            var_dump($e->getLastException(), $e->getLastRequest());
        }
    }


   public static function download($url, $filename='')
    {
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'aliPic' . DIRECTORY_SEPARATOR;
        var_dump('下载图片');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        $file = curl_exec($ch);
        curl_close($ch);
        $resource = fopen($filename, 'a');
        fwrite($resource, $file);
        fclose($resource);
        return $filename;
    }

}